package com.naza.rpc.consumer.balance;

import com.naza.rpc.model.ServiceInstance;

import java.util.*;

/**
 * 随机 路由
 * 
 * @author yl
 */
public class RandomLoadBalancer implements LoadBalancer {
    public static final RandomLoadBalancer INSTANCE = new RandomLoadBalancer();

    private static final int DEFAULT_WEIGHT = 5;

    private Random random = new Random();

    @Override
    public ServiceInstance selectClient(String serviceName, List<ServiceInstance> candidates) {
        if (candidates == null || candidates.isEmpty()) {
            return null;
        }
        //TODO:考虑权重?
        int candidatesSize = candidates.size();
        return candidates.get(random.nextInt(candidatesSize));
    }
}
